<?php

/*

Desktopd Colors
Copyright (C) 2015  Desktopd Developer(s)

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

https://www.gnu.org/licenses/agpl.html

*/


namespace Desktopd\Colors;


class CIELUVPlot {
    protected $colors = array();
    
    protected $path_LU = false;
    protected $path_LV = false;
    protected $path_UV = false;
    
    protected $L_min;
    protected $L_max;
    protected $u_min;
    protected $u_max;
    protected $v_min;
    protected $v_max;
    
    public function __construct() {
        
    }
    
    public function addColor(CIELUV $color) {
        $this->colors[] = $color;
        
        $L = $color->getL();
        $u = $color->get_u();
        $v = $color->get_v();
        
        $this->L_min = isset($this->L_min) ? min($this->L_min, $L) : $L;
        $this->L_max = isset($this->L_max) ? max($this->L_max, $L) : $L;
        
        $this->u_min = isset($this->u_min) ? min($this->u_min, $u) : $u;
        $this->u_max = isset($this->u_max) ? max($this->u_max, $u) : $u;
        
        $this->v_min = isset($this->v_min) ? min($this->v_min, $v) : $v;
        $this->v_max = isset($this->v_max) ? max($this->v_max, $v) : $v;
        
        
    }
    
    public function saveLU($path) {
        $this->path_LU = $path;
    }
    
    public function saveLV($path) {
        $this->path_LV = $path;
    }
    
    public function saveUV($path) {
        $this->path_UV = $path;
    }
    
    
    public function render($minWidth = 4096, $minHeight = 2560) {
        if ($this->path_LU !== false) {
            $LU_plot = new Plot(
                $minWidth,
                $minHeight,
                array($this->u_max, $this->u_min),
                array($this->L_max, $this->L_min)
            );
        }
        
        if ($this->path_LV !== false) {
            $LV_plot = new Plot(
                $minWidth,
                $minHeight,
                array($this->v_max, $this->v_min),
                array($this->L_max, $this->L_min)
            );
        }
        
        if ($this->path_UV !== false) {
            $UV_plot = new Plot(
                $minWidth,
                $minHeight,
                array($this->u_max, $this->u_min),
                array($this->v_max, $this->v_min)
            );
        }
        
        $whitepoint = (new sRGB(1, 1, 1))->toXYZ();
        
        foreach ($this->colors as $color) {
            $L = $color->getL();
            $u = $color->get_u();
            $v = $color->get_v();
            
            $sRGB = sRGB::fromXYZ($color->toXYZ($whitepoint));
            
            if ($this->path_LU !== false) {
                $LU_plot->plot($u, $L, $sRGB);
            }
            
            if ($this->path_LV !== false) {
                $LV_plot->plot($v, $L, $sRGB);
            }
            
            if ($this->path_UV !== false) {
                $UV_plot->plot($u, $v, $sRGB);
            }
        }
        
        if ($this->path_LU !== false) {
            $LU_plot->save($this->path_LU);
        }
        
        if ($this->path_LV !== false) {
            $LV_plot->save($this->path_LV);
        }
        
        if ($this->path_UV !== false) {
            $UV_plot->save($this->path_UV);
        }
        
        return true;
    }
    
    public function __invoke() {
        return $this->render();
    }
    
    public function __toString() {
        return "L: [{$this->L_min}, {$this->L_max}], U: [{$this->u_min}, {$this->u_max}], V: [{$this->v_min}, {$this->v_max}]";
    }
}


// vim: ts=4 et ai

